(function(){
	if(!window.cell) window.cell={};
	if(!window.cell.console) window.cell.console={
		overwrite: true,
		svcConsole: Components.classes['@mozilla.org/consoleservice;1'].getService(Components.interfaces.nsIConsoleService),
		log:function(msg){
			this.svcConsole.logStringMessage(msg);
		},
	};
	if(!window.cell.file) window.cell.file= function(path,file) {
		if ( window == this || !this.init ){
			return new cell.file(path,file);
		}
		return this.init(path,file);
	}
	cell.file.fn = cell.file.prototype = {
		version: "2.0.0",
		preset: new Array("ProfD","DefProfRt","UChrm","DefRt","PrfDef","ProfDefNoLoc","APlugns","AChrom","ComsD","CurProcD","Home","TmpD","ProfLD","resource:app"),
		svcFilePicker: Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker),
		svcLocalFile: Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile),
		svcFOS: Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream),
		svcFile: function(val){
			return Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get(val, Components.interfaces.nsIFile).path;
		},
		file: null,
		init: function(path,file){
			var LocalFile = this.svcLocalFile;
			if(path){
				for (var i=0; i<this.preset.length; i++){
					if(path==this.preset[i]){
						var path=this.svcFile(path);
					}
				}
				LocalFile.initWithPath(path);
			}
			if(file){LocalFile.append(file);}
			this.file=LocalFile;
			return this;
		},
		picker: function(arg){
			if(!arg) var arg={};
			if(!arg.title) arg.title="File Picker";
			
			var fp = this.svcFilePicker;
			
			switch (arg.mode){
				case "save":
					arg.mode=this.svcFilePicker.modeSave;
					break;
				case "folder":
					arg.mode=this.svcFilePicker.modeGetFolder;
					break;
				case "multiple":
					arg.mode=this.svcFilePicker.modeOpenMultiple;
					break;
				case "open":
				default:
					arg.mode=this.svcFilePicker.modeOpen;
					break;
			}
			
			fp.init(window,"test",arg.mode);
			var result = fp.show();
			
			switch (result){
				case this.svcFilePicker.returnOK:
					this.file=fp.file;
					break;
				case this.svcFilePicker.returnReplace:
					this.file=fp.file;
					break;
				default:
				case this.svcFilePicker.returnCancel:
					break;
			}
			return this;
		},
		read:function(arg){
			if(!arg) var arg={};
			if(!arg.encode) {arg.encode="UTF-8";}
			var FileInputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
			var ConverterInputStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);
			
			FileInputStream.init(this.file, -1, 0, 0);
			ConverterInputStream.init(FileInputStream, arg.encode , 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
			var str = {};
			var data="";
			while (ConverterInputStream.readString(4096, str) != 0) {
				data += str.value;
				cell.console.log(str.value)
			}
			ConverterInputStream.close();
			return data;
		},
		write:function(content,arg){
			if(!arg) var arg={};
			if(!arg.encode) {arg.encode="UTF-8";}

			if (this.file.exists()){this.file.remove(true);}
			this.file.create(this.file.NORMAL_FILE_TYPE, 0666);
				
			var FOS = this.svcFOS;
			FOS.init(this.file, 0x02, 0664, 0);
			
			var cosObj = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
			cosObj.init(FOS, arg.encode, 0, 0x0000);
			cosObj.writeString(content);
			cosObj.close();
		}, 
	}
})();